using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Android.Support.V4.App;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;

using Android.Widget;
using System.Collections;
using CPXamarinPortable;

namespace CPXamarin
{
    public class ArrayFragment : ScrollTabHolderFragment,AbsListView.IOnScrollListener
    {
        private static  String ARG_POSITION = "position";
        private ListView mListView;
        string[] items;

        private int mPosition;


        public ArrayFragment()
        {

        }

        public static Android.Support.V4.App.Fragment NewInstance(int position)
        {
            ArrayFragment f = new ArrayFragment();
            Bundle b = new Bundle();
            b.PutInt(ARG_POSITION, position);
            f.Arguments = b;
            return f;
        }


        public  override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            mPosition = Arguments.GetInt(ARG_POSITION);
            items = new string[] { "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers", "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers", "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers", "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers" };
          

    
            // Set our view from the "main" layout resource
        }
        public  override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View view = inflater.Inflate(Resource.Layout.ListViewFragment, null);
            mListView = view.FindViewById<ListView>(Resource.Id.list);

            View placeHolderView = inflater.Inflate(Resource.Layout.view_header_placeholder, mListView, false);
            mListView.AddHeaderView(placeHolderView);

           

          
            return view;
        }


        public async override void OnActivityCreated(Bundle savedInstanceState)
        {
            base.OnActivityCreated(savedInstanceState);
            mListView.SetOnScrollListener(this);
            if (mPosition == 0)
            {
                var progressDialog = ProgressDialog.Show(this.Activity, "Please wait...", "Retriving data", true);
                var result = await CPAuth.MyProfile();
                mListView.Adapter = new CustomListProfileAdapter(Activity, result);
                progressDialog.Hide();
                progressDialog.Dismiss();
            }
            else
            {
                mListView.Adapter = new ArrayAdapter<String>(Activity, Resource.Layout.list_item, Resource.Id.text1, items);
            }
           
        }


       


        public new void AdjustScroll(int scrollHeight)
        {
            if (scrollHeight == 0 && mListView.FirstVisiblePosition >= 1)
            {
                return;
            }
            mListView.SetSelectionFromTop(1, scrollHeight);
        }

        void AbsListView.IOnScrollListener.OnScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
        {
            if (base.mScrollTabHolder != null)
                base.mScrollTabHolder.OnScroll(view, firstVisibleItem, visibleItemCount, totalItemCount, mPosition);
        }

        void AbsListView.IOnScrollListener.OnScrollStateChanged(AbsListView view, ScrollState scrollState)
        {
           
        }

       
    }
}